package com.weiweiqin.controller.back;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weiweiqin.model.Brands;
import com.weiweiqin.service.BrandsService;
import com.weiweiqin.service.GoodsService;
import com.weiweiqin.vo.common.CategoryVO;
import com.weiweiqin.vo.common.Page;
import com.weiweiqin.vo.common.Result;

@Controller
@RequestMapping("admin/brands")
public class BrandsController {

	private static final Logger logger = LoggerFactory.getLogger(BrandsController.class);
	@Autowired
	private BrandsService brandsService;	
	@Autowired
	private GoodsService goodsService;

	

	@RequestMapping(value = "/add.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String add(Brands brands) {
		Result result = new Result();
		logger.error("Enter method add...,brands="+brands);
		
		brands.setUpdateDate(new Date());
		try {
			Brands isExist = brandsService.getByName(brands.getName());
			if(null != isExist){
				result.setResult(false);
				result.setErrorMsg("已经存在该名称!");
				logger.error("已经存在该名称!");
				return result.toJson();
			}
			brandsService.save(brands);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
	
	@RequestMapping(value = "/added.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String added(int id) {
		Result result = new Result();
		try {
			Brands brands = brandsService.get(id);
			if(brands.getAdded() == 0){
				brands.setAdded(1);
			}else{
				brands.setAdded(0);
			}
			brands.setUpdateDate(new Date());
			brandsService.update(brands);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
	
	@RequestMapping(value = "/orderKey.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String orderKey(int id,int orderKey) {
		Result result = new Result();
		try {
			Brands brands = brandsService.get(id);
			brands.setOrderKey(orderKey);
			brandsService.update(brands);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
	

	@RequestMapping(value = "/edit.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String edit(Brands brands) {
		logger.info("Enter method edit...,brands="+brands);
		Result result = new Result();
		brands.setUpdateDate(new Date());
		try {
			brandsService.update(brands);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}

	@RequestMapping(value = "/get.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String get(int id) {
		Result result = new Result();
		try {
			Brands brands = brandsService.get(id);
			result.setObj(brands);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}

	@RequestMapping(value = "/getAll.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String getAll() {
		Result result = new Result();
		try {
			List<Brands> brandsList = brandsService.getAll();
			result.setObj(brandsList);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
	
	@RequestMapping(value = "/getAlllAddOn.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String getAllAddOn() {
		Result result = new Result();
		try {
			List<Brands> brandsList = brandsService.getAllAddOn();
			result.setObj(brandsList);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}

	@RequestMapping(value = "/del.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String del(int id) {
		logger.info("Enter method del...,id="+id);
		Result result = new Result();
		try {
			brandsService.del(id);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}

	@RequestMapping(value = "/pageList.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String pageList(int currPage, HttpServletResponse response)
			throws UnsupportedEncodingException {

		Result result = new Result();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Page page = new Page();
			page.setCurrPage(currPage);
			List<Brands> brandsList = brandsService.pageList(page, null);

			List<CategoryVO> list = new ArrayList<CategoryVO>();
			if (null != brandsList && !brandsList.isEmpty()) {

				for (Brands brands : brandsList) {
					CategoryVO vo = new CategoryVO();
					vo.setId(brands.getId());
					vo.setName(brands.getName());
					vo.setUpdateDate(sdf.format(brands.getUpdateDate()));
					vo.setAdded(brands.getAdded());
					vo.setOrderKey(brands.getOrderKey());
					vo.setPic(brands.getPic());
					list.add(vo);
				}
			}
			int totalCount = brandsService.totalCount(null);
			Map<String, Object> mp = new HashMap<String, Object>();
			mp.put("list", list);
			mp.put("totalCount", totalCount);
			result.setObj(mp);
		} catch (Exception e) {
			logger.error("数据库异常...", e);	
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
}
